import { defineEventHandler } from 'h3';

export function createSystemProxyStatusApi(app) {
  return defineEventHandler(async (event) => {
    if (event.node.req.method === 'GET') {
      const status = app.systemProxyManager.getStatus();
      const proxyStatus = await app.systemProxyManager.getProxyStatus();
      
      return {
        ...status,
        current: proxyStatus
      };
    }
  });
}

export function createSystemProxySetApi(app) {
  return defineEventHandler(async (event) => {
    if (event.node.req.method === 'POST') {
      try {
        const success = await app.systemProxyManager.setSystemProxy();
        return { success, message: success ? '系统代理设置成功' : '系统代理设置失败' };
      } catch (error) {
        event.node.res.statusCode = 500;
        return { success: false, message: error.message };
      }
    }
  });
}

export function createSystemProxyRestoreApi(app) {
  return defineEventHandler(async (event) => {
    if (event.node.req.method === 'POST') {
      try {
        const success = await app.systemProxyManager.restoreSystemProxy();
        return { success, message: success ? '系统代理恢复成功' : '系统代理恢复失败' };
      } catch (error) {
        event.node.res.statusCode = 500;
        return { success: false, message: error.message };
      }
    }
  });
}

export function createSystemProxyEmergencyRestoreApi(app) {
  return defineEventHandler(async (event) => {
    if (event.node.req.method === 'POST') {
      try {
        await app.systemProxyManager.emergencyRestore();
        return { success: true, message: '应急恢复成功' };
      } catch (error) {
        event.node.res.statusCode = 500;
        return { success: false, message: error.message };
      }
    }
  });
} 